<?php
namespace app\zhihui\controller;

use app\common\model\ZhSupportReply as ZhSupportReplyModel;
use app\common\model\ZhSupportReplyEducation as ZhSupportReplyEducationModel;
use app\common\model\Member as MemberModel;
use app\home\controller\Base;

/**
 * 答疑解惑回答控制器
 * Class Zhsupport
 * @package app\index\controller
 */
class Zhsupportreply extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new ZhSupportReplyModel;
        $mmodel = new MemberModel;
        $emodel = new ZhSupportReplyEducationModel;
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try{
            $map['sid'] = $param['id'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size,'id,mid,content,create_time');
            foreach($lists as &$v){
                $member = $mmodel->findData($v['mid'],'id','img,username');
                $http = substr($member['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('domain_name').$member['img'];
                }else{
                    $v['img'] = $member['img'];
                }
                $v['username'] = $member['username'];
                $education_num = $emodel->getCount(['d.rid'=>$v['id']]);
                $v['education_num'] = $education_num;
                $education = $emodel->findOne(['mid'=>$user['id'],'rid'=>$v['id']]);
                if(empty($education)){
                    $v['is_education'] = 2;
                }else{
                    $v['is_education'] = 1;
                }
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }

    /**
     * [add  添加]
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new ZhSupportReplyModel;
        $user = $this->user;
        try{
            $param['mid'] = $user['id'];
            $model->addData($param);
        }catch(\Exception $e){
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '添加成功');
    }
}